Skip to content

fix(hibernate5): an association block that is a branch of or keeps the entities without the association - #16931

Open
borinquenkid wants to merge 7 commits into
7.0.xfrom
fix/h5-or-association-block-7.0.x
Open

borinquenkid wants to merge 7 commits into
7.0.xfrom
fix/h5-or-association-block-7.0.x

Conversation

@borinquenkid

Copy link
Copy Markdown
Member

An association block inside or { } is one branch of the disjunction. The criteria guide documents it that way: "all accounts that have either performed transactions in the last 10 days OR have been recently created in the last 10 days". The block was an inner join, so an account without transactions was dropped before the other branch could match it.

A block with no join type of its own, inside an or, is now a left join. createAlias is unchanged: the docs say it is an inner join unless a join type is given.

Tests: new shared TCK specs CriteriaOrAssociationSpec (instance, isNull, isNotNull in or: the same on every binding) and CriteriaOrAssociationBlockSpec (block and to-many block in or; MongoDB skipped, it has no association blocks). Full Hibernate 5 suite passes, codeStyle is clean. CriteriaOrAssociationBlockSpec also runs on Neo4j and passes there with #16836, so merge #16836 first.

Fixes #16924
Evidence and the doc passage: #16924 (comment)

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

…e entities without the association

The criteria guide documents an association block inside or as one branch:
accounts that have either performed transactions OR were recently created.
The block was an inner join, so an account without transactions was dropped
before the other branch could match it. A block that has no join type of its
own, inside an or, is now a left join.

Fixes #16924

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not approving. An association block that accepts a null now matches an entity that has no association. A later explicit inner join also reuses that left join.

…ociated entity

A null test in the block matched the entities without the association, because the
left join gives null for the missing one. The block now also requires the id of the
joined alias. The shared block cases skip Neo4j until #16836 is merged.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A null test in an association block no longer matches an entity that has no association. Thread PRRT_kwDOAAfRJ862Qfia resolved. Not approving. An empty association block no longer requires the association, and a later inner join reuses the left join.

…uires an associated entity, and an empty block does too

A block outside an or that reuses the left join of a block in one no longer returns the entities without the
association, and an empty block in an or keeps requiring an associated entity of the whole entity, as its inner join did.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An empty association block requires the association again, and a later inner join no longer reuses the left join as if the child were optional. Threads PRRT_kwDOAAfRJ862VrVg and PRRT_kwDOAAfRJ862VrVl resolved. Not approving. A later block that asks for a left join is treated as if it required the child.

…r not an or joined the association with a left join

or { child {...}; eq(...) }; child(LEFT_JOIN) { isNull('name') } keeps the entity without a child, as the left join the
second block asked for does.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A later block that asks for a left join keeps that left join. Not approving. A later block that asks for an inner join still reuses the left join.

// a block that is a branch of an "or" asks for an associated entity, and so does a block that
// reuses the left join of one, as a block outside an "or" always did
// (a join type the caller named is kept as the caller named it)
final boolean requiresAssociated = branchOfOr || (!hasMoreThanOneArg && leftBlockAliases.contains(blockAlias));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or { child { eq('name', 'kid') }; eq('name', 'orphan') }; child(CriteriaSpecification.INNER_JOIN) { isNull('name') }, where orphan has no child. The list returns orphan. The later block asked for an inner join. Require the child.

@borinquenkid
borinquenkid requested a review from matrei November 12, 2026 14:12
…and is no branch of an or

CriteriaOrAssociationBlockSpec expects an association block without criteria
to leave out the entities that have no associated entity, and to add no
branch to the or it stands in, as on the datastores that join. The in-memory
store matched nothing for the block and let the other branch of the or find
the orphan.

The block is now skipped as a branch and the whole result is restricted to
the entities that have an associated entity.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@jamesfredley jamesfredley left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. An explicit inner join after an or reuses the left join, so an entity without the association is kept.

// a block that is a branch of an "or" asks for an associated entity, and so does a block that
// reuses the left join of one, as a block outside an "or" always did
// (a join type the caller named is kept as the caller named it)
final boolean requiresAssociated = branchOfOr || (!hasMoreThanOneArg && leftBlockAliases.contains(blockAlias));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with has a child named kid. orphan has no child. or { child { eq('name', 'kid') }; eq('name', 'orphan') } then child(INNER_JOIN) {} should return only with. It returns with and orphan. The second block reuses the left join from the or, and the extra argument turns off the existence check. Keep the inner join when the caller asks for one.

…d a block in an or of a detached criteria keeps its left join

child(INNER_JOIN) { } after an or reused the left join the or had made for the same association and, as it named a join
type, skipped the existence check, so an entity without the association stayed in the result; a block that names an
inner join asks for an associated entity now. The query path of a detached criteria had no rule for a block that is a
branch of an or at all, so an entity without the association dropped out of the or, an empty block did not ask for an
associated entity and a block after the or did not either; it makes a left join for such a block, checks for an
associated entity as criteria does, and does not count a negation as an or. CriteriaOrAssociationBlockSpec runs
each row through criteria and through a detached criteria, and has the explicit join type rows.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley Fixed in the new commit.

Your case. or { child { eq('name', 'kid') }; eq('name', 'orphan') } followed by child(INNER_JOIN) {} returned with and orphan: the second block reused the left join the or had made for child, and because it named a join type it skipped the existence check. A block that names an inner join asks for an associated entity now when it reuses such a join, so only with comes back. CriteriaOrAssociationBlockSpec has it with an empty block, with eq('name', 'kid') in it, and an explicit inner join inside the or, which stays inner. An explicit left join after the or is kept as the caller named it (child(LEFT_JOIN) { isNull('name') } finds orphan).

The same rule through the other entry point. I ran every row of that spec through a detached criteria as well, and four of the eight earlier rows failed there: the query path used by detached criteria had no rule for a block that is a branch of an or, so an entity without the association dropped out of the or, an empty block did not ask for an associated entity, and neither did a block after the or. It makes a left join for such a block now, checks for an associated entity the way the criteria builder does, and does not count a not { } as an or. A where query already kept the entity. A detached criteria takes an alias as the first argument of a block, not a join type, so the rows that give a join type run through criteria only, and on Hibernate only (the in-memory store has no such argument).

The H5 module (705 tests), the in-memory store and codeStyle pass; five rows fail without the change.

borinquenkid added a commit that referenced this pull request Nov 17, 2026
…nd a path a block repeats keeps one join

getOrCreateAlias reused the join of any earlier block of the same name whenever the block came with an alias, which a
block of a detached criteria always does, so dam { sire { ... } } filtered on the join of the root sire block and
returned nothing, and a second dam.sire block asked Hibernate for the same association path twice and failed with
a duplicate association path. The join made for the very path of the block is reused now, and a join made for the
same name under another path is not. The pedigree shapes of CriteriaNestedShapesSpec are checked through a detached
criteria as well, with the repeated nested blocks; the blocks that are branches of an or stay on criteria until #16931.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
borinquenkid added a commit that referenced this pull request Nov 17, 2026
…d criteria

The detached criteria path keeps a left join for a block that is a branch of an or since #16931 (merged into this
branch), so the three shapes that were checked through criteria only are checked through both.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. An explicit inner join after one or is kept. The same join inside a second or still lets an entity without the association through.

}
else if (requiresAssociated) {
// a missing associated entity is a null that a null test in the block would match
logicalExpression.args.add(0, Restrictions.isNotNull(blockAlias + ".id"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with has a child named kid. orphan has no child. An or that asks for the child, then a second or with child(0), should return only with. It returns with and orphan. The existence check sits inside the second or, so the name branch bypasses it. Keep that check on the whole query.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants